layui.use(['table', 'admin', 'ax', 'func', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var func = layui.func;

    /**
     * 版本号管理
     */
    var AppVersion = {
        tableId: "appVersionTable"
    };

    /**
     * 初始化表格的列
     */
    AppVersion.initColumn = function () {
        return [[
            {field: 'version', sort: true, title: '版本号', width: 120},
            {
                align: 'center', templet: function (d) {
                    var type = d.type;
                    if (type == 1) {
                        return '正式版'
                    }else {
                        return '测试版'
                    }
                }, title: '版本类型', width: 120, unresize: true
            },
            {field: 'sort', sort: true, title: '排序', width: 80},
            {field: 'delFlag', sort: true, templet: '#statusTpl', title: '状态', width: 100},
            {field: 'createTime', sort: true, title: '创建时间', width: 180},
            {field: 'updateTime', sort: true, title: '修改时间', width: 180},
            {field: 'createName', sort: true, title: '创建者', width: 120},
            {field: 'updateName', sort: true, title: '修改者', width: 120},
            {align: 'center', toolbar: '#tableBar', title: '操作', width: 120}
        ]];
    };

    /**
     * 点击查询按钮
     */
    AppVersion.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(AppVersion.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    AppVersion.openAddDlg = function () {
        func.open({
            title: '添加版本号',
            content: Feng.ctxPath + '/appVersion/add',
            tableId: AppVersion.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    AppVersion.openEditDlg = function (data) {
        func.open({
            title: '修改版本号',
            content: Feng.ctxPath + '/appVersion/edit?id=' + data.id,
            tableId: AppVersion.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    AppVersion.exportExcel = function () {
        var checkRows = table.checkStatus(AppVersion.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    AppVersion.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/appVersion/delete", function (data) {
                if (data.code == 200) {
                    Feng.success("删除成功!");
                }else{
                    Feng.error("删除失败!" + data.message + "!");
                }
                table.reload(AppVersion.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + AppVersion.tableId,
        url: Feng.ctxPath + '/appVersion/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: AppVersion.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        AppVersion.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        AppVersion.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        AppVersion.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + AppVersion.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            AppVersion.openEditDlg(data);
        } else if (layEvent === 'delete') {
            AppVersion.onDeleteItem(data);
        }
    });

    form.on('switch(delFlag)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

         AppVersion.changeDelFlag(id, checked);
    });

    /**
     * 修改状态
     *
     * @param userId 用户id
     * @param checked 是否选中（true,false），选中就是解锁用户，未选中就是锁定用户
     */
     AppVersion.changeDelFlag = function (id, checked) {
        if (checked) {
            var ajax = new $ax(Feng.ctxPath + "/appVersion/enable", function (data) {
                if (data.code == 200) {
                    Feng.success("启用成功!");
                }else{
                    Feng.error("启用失败!" + data.message + "!");
                }
                table.reload( AppVersion.tableId);
            }, function (data) {
                Feng.error("启用失败!" + data.message + "!");
                table.reload( AppVersion.tableId);
            });
            ajax.set("id", id);
            ajax.start();
        } else {
            var ajax = new $ax(Feng.ctxPath + "/appVersion/disable", function (data) {
                if(data.code == 200) {
                    Feng.success("禁用成功!");
                }else{
                    Feng.error("禁用失败!" + data.message + "!");
                }
                table.reload( AppVersion.tableId);
            }, function (data) {
                Feng.error("禁用失败!" + data.message + "!");
                table.reload( AppVersion.tableId);
            });
            ajax.set("id", id);
            ajax.start();
        }
    };
});
